package com.example.lovedogs.info;

import java.util.ArrayList;
import java.util.List;




















import com.example.lovedogs.R;
import com.example.lovedogs.R.color;
import com.example.lovedogs.R.id;
import com.example.lovedogs.R.layout;
import com.example.lovedogs.fragment.Info01Fragment;
import com.example.lovedogs.fragment.Info02Fragment;
import com.example.lovedogs.fragment.Info03Fragment;
import com.example.lovedogs.fragment.Info04Fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class InfoActivity extends FragmentActivity implements OnClickListener{
	class MyFragmentPagerAdapter extends FragmentPagerAdapter{
		ArrayList<Fragment> list;
		public MyFragmentPagerAdapter(FragmentManager fm,ArrayList<Fragment> list) {
			super(fm);
			this.list = list;
			
		}

		@Override
		public int getCount() {
			return list.size();
		}
		
		@Override
		public Fragment getItem(int arg0) {
			return list.get(arg0);
		}
		
	}
	
	
	private TextView tv_info_sixin,tv_info_zan,tv_info_pinglun,tv_info_tongzhi;
	private ViewPager vp_info;
	private ArrayList<Fragment> fragmentList;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_info);
		tv_info_sixin=(TextView) findViewById(R.id.tv_info_sixin);
		tv_info_zan=(TextView) findViewById(R.id.tv_info_zan);
		tv_info_pinglun=(TextView) findViewById(R.id.tv_info_pinglun);
		tv_info_tongzhi=(TextView) findViewById(R.id.tv_info_tongzhi);
		vp_info=(ViewPager) findViewById(R.id.vp_info);
		
		selected_sixin();//默认初始选中
		tv_info_sixin.setOnClickListener(this);
		tv_info_zan.setOnClickListener(this);
		tv_info_pinglun.setOnClickListener(this);
		tv_info_tongzhi.setOnClickListener(this);
		
		//viewpager适配
/*		List<View> views=new ArrayList<View>();
		
		//将xml界面转换为view对象
		LayoutInflater inflater = getLayoutInflater();
		View view1 = inflater.inflate(R.layout.vp_info_pager1, null);
		views.add(view1);
		View view2 = inflater.inflate(R.layout.vp_info_pager2, null);
		views.add(view2);
		View view3 = inflater.inflate(R.layout.vp_info_pager3, null);
		views.add(view3);
		View view4 = inflater.inflate(R.layout.vp_info_pager4, null);
		views.add(view4);
		
		MyAdapter adapter=new MyAdapter(views);
		vp_info.setAdapter(adapter);
		*/
		
		fragmentList=new ArrayList<Fragment>();
		Fragment fragment1=new Info01Fragment();
		Fragment fragment2=new Info02Fragment();
		Fragment fragment3=new Info03Fragment();
		Fragment fragment4=new Info04Fragment();
		
		fragmentList.add(fragment1);
		fragmentList.add(fragment2);
		fragmentList.add(fragment3);
		fragmentList.add(fragment4);
		
		vp_info.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentList));
		//
		vp_info.addOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageScrollStateChanged(int arg0) {
				
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				
			}

			@Override
			public void onPageSelected(int position) {
				switch (position) {
				case 0:
					selected_sixin();
					break;
				case 1:
					selected_zan();
					break;
				case 2:
					selected_pinglun();
					break;
				case 3:
					selected_tongzhi();
					break;

				default:
					break;
				}
				
			}
			
		});
		
		
	}

	private void setTV_Style(TextView tv){
		tv.setBackgroundColor(getResources().getColor(R.color.title01));
		tv.setTextColor(getResources().getColor(R.color.white));
	}
	private void setTV_Selected_Style(TextView tv){
		tv.setBackgroundColor(getResources().getColor(R.color.white));
		tv.setTextColor(getResources().getColor(R.color.title01));
	}
	private void selected_sixin(){
		setTV_Selected_Style(tv_info_sixin);
		setTV_Style(tv_info_zan);
		setTV_Style(tv_info_pinglun);
		setTV_Style(tv_info_tongzhi);
	}
	private void selected_zan(){
		setTV_Style(tv_info_sixin);
		setTV_Selected_Style(tv_info_zan);
		setTV_Style(tv_info_pinglun);
		setTV_Style(tv_info_tongzhi);
	}
	private void selected_pinglun(){
		setTV_Style(tv_info_sixin);
		setTV_Style(tv_info_zan);
		setTV_Selected_Style(tv_info_pinglun);
		setTV_Style(tv_info_tongzhi);
	}
	private void selected_tongzhi(){
		setTV_Style(tv_info_sixin);
		setTV_Style(tv_info_zan);
		setTV_Style(tv_info_pinglun);
		setTV_Selected_Style(tv_info_tongzhi);
	}

	@Override
	public void onClick(View v) {
		int id=v.getId();
		switch (id) {
		case R.id.tv_info_sixin:
			selected_sixin();
			vp_info.setCurrentItem(0);
			break;
		case R.id.tv_info_zan:
			selected_zan();
			vp_info.setCurrentItem(1);
			break;
		case R.id.tv_info_pinglun:
			selected_pinglun();
			vp_info.setCurrentItem(2);
			break;
		case R.id.tv_info_tongzhi:
			selected_tongzhi();
			vp_info.setCurrentItem(3);
			break;

		default:
			break;
		}
	}
	
	class MyAdapter extends PagerAdapter{
		private List<View> views;
		public MyAdapter(List<View> views) {
			this.views=views;
		}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return views.size();
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			// TODO Auto-generated method stub
			return view==object;
		}
		
		@Override
		public void destroyItem(View container, int position, Object object) {
			ViewPager vp=(ViewPager) container;
			View view=views.get(position);
			vp.removeView(view);
		}
		
		@Override
		public Object instantiateItem(View container, int position) {
			View view=views.get(position);
			ViewPager vp=(ViewPager) container;
			vp.addView(view);
			return view;
		}
	}
}
